#include <bits/stdc++.h>

using namespace std;

// #define LOCK_GETCHAR
// #define USE_INT_128

#if __cplusplus < 201400
#warning "Please use c++14 or higher."
#define CONSTEXPR_FUNC
#define ENABLE_IF_INT
#else
#define CONSTEXPR_FUNC constexpr
#define ENABLE_IF_INT , enable_if_t<_is_integer<T>, int> = 0
template <class T> constexpr bool _is_integer = numeric_limits<T>::is_integer;
template <> constexpr bool _is_integer<bool> = false;
template <> constexpr bool _is_integer<char> = false;
#ifdef USE_INT_128
template <> constexpr bool _is_integer<__int128> = true;
template <> constexpr bool _is_integer<__uint128_t> = true;
#endif
#endif

#if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(LOCK_GETCHAR)
#define getchar getchar_unlocked
#endif

#define il inline
#define mkp make_pair
#define fi first
#define se second
#define ssz(x) (signed((x).size()))
#define _loop_i_t(j, k) make_signed_t<decltype((j) - (k))>
#define For(i, j, k) for (_loop_i_t(j, k) i = (j); i <= (k); ++i) // NOLINT
#define ForDown(i, j, k)                                                       \
  for (_loop_i_t(j, k) i = (j); i >= decltype(i)(k); --i) // NOLINT
#define eb emplace_back
#ifndef ONLINE_JUDGE
#define FileIO(filename)                                                       \
  freopen(filename ".in", "r", stdin);                                         \
  freopen(filename ".out", "w", stdout)
#else
#define FileIO(filename) void(0)
#endif

using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#ifdef USE_INT_128
using lll = __int128_t;
using ulll = __uint128_t;
#endif

// clang-format off
CONSTEXPR_FUNC il ll qpow(ll x, ull y, ll mod){ ll ans = 1; x %= mod; while (y) { if (y & 1) (ans *= x) %= mod; (x *= x) %= mod; y >>= 1; } return ans; }
template<typename T> CONSTEXPR_FUNC il void cmin(T & x, const T &y){ x = min(x, y); }
template<typename T> CONSTEXPR_FUNC il void cmax(T & x, const T &y){ x = max(x, y); }
template<typename T ENABLE_IF_INT> il void read(T &x){ x = 0; int f = 1; int c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + (c - '0'); c = getchar(); } x *= f; }
template<typename T, typename ... Args> il void read(T &x, Args &... y){ read(x), read(y...); }
// clang-format on

// File head end

namespace {
constexpr ll MAXN = 2e5 + 5;
int n, q, c, cnt, a[MAXN], fa[MAXN][18], col[MAXN][18], dfn[MAXN], seq[MAXN],
    sz[MAXN], Ans[MAXN], sum[1 << 9][(1 << 6) + 3];
vector<int> T[MAXN], ins[MAXN];
vector<pii> Qry[MAXN];

void dfs(int x) {
  seq[dfn[x] = ++cnt] = x, col[x][0] = 1 << a[x], sz[x] = 1;
  For(i, 1, 17) {
    fa[x][i] = fa[fa[x][i - 1]][i - 1];
    col[x][i] = col[x][i - 1] | col[fa[x][i - 1]][i - 1];
  }
  {
    int cur = col[x][0], p = x;
    while (p) {
      ins[p].eb(cur);
      ForDown(i, 17, 0) if ((cur | col[p][i]) == cur) p = fa[p][i];
      ins[p].eb(-cur), cur |= col[p][0];
    }
  }
  for (int v : T[x])
    dfs(v), sz[x] += sz[v];
}
void add(int S, int v) {
  assert(S > 0);
  int w = S >> 6, o = S & ((1 << 6) - 1);
  For(i, 0, (1 << 6) - 1) sum[w][i] += ((o | i) == i) ? v : 0;
}
int qry(int S) {
  assert(S > 0);
  int ans = 0, w = S >> 6, o = S & ((1 << 6) - 1);
  int msk = (1 << max(0, c - 6)) - 1;
  For(i, 0, msk) ans += ((w | i) == w) ? sum[i][o] : 0;
  return ans;
}

void Main() {
  FileIO("tree");
  read(n, q, c);
  For(i, 1, n) read(a[i]);
  For(i, 2, n) read(fa[i][0]), T[fa[i][0]].eb(i);
  dfs(1);
  For(i, 1, q) {
    int S, u;
    read(S, u);
    if (!(S >> a[u] & 1)) {
      Ans[i] = 1;
      continue;
    }
    ForDown(j, 17, 0) if (fa[u][j] && (S | col[fa[u][0]][j]) == S) u = fa[u][j];
    // cerr << "Q " << u << ' ' << dfn[u] + sz[u] - 1 << '\n';
    assert(u), Qry[dfn[u] - 1].eb(S, -i), Qry[dfn[u] + sz[u] - 1].eb(S, i);
  }
  For(i, 1, n) {
    int x = seq[i];
    for (int S : ins[x])
      if (S > 0)
        add(S, 1);
      else
        add(-S, -1);
    for (auto [S, id] : Qry[i]) {
      int v = qry(S);
      // cerr << i << ' ' << id << ' ' << S << ' ' << v << '\n';
      (id > 0) ? (Ans[id] += v) : (Ans[-id] -= v);
    }
  }
  For(i, 1, q) cout << Ans[i] << '\n';
}
} // namespace

signed main() { return Main(), 0; }
